package com.yuren.codecrushlearn.Q5100;

/**
 * Created with Intellij IDEA.
 * Description:
 *
 * @author Sleepy Code Tom
 * @date 2025-04-01 23:58
 */
public class Q5151 {
    public void bubbleSort(int[] arr, int n) {
        if (n <= 1) return;
        //n-1遍即可
        for (int i = 0; i < n - 1; i++) {
            //提前退出标志
            boolean flag = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flag = true;
                }
            }
            if (!flag) break;
        }
    }
}
